// cd /projects/data_commons/cw_code/
// qstata cw_ind_sect_2_fk_imp.do &

global dir_proj = "/projects/data_commons/"
global dir_cw = "${dir_proj}/cw/"
global dir_lbd = "${dir_proj}/lbd/"

di "Started at $S_DATE $S_TIME"

cd ${dir_cw}/

//==============================================================================

global ds_sect "cw_ind_sect"

import delimited "cw_ind_fk_raw_man.csv", clear varn(1)
drop if year_min == 2015 // Haven't fixed naics12

do /projects/data_commons/cw_code/cw_ind_1_jsung_fix.do
f_jsung_naics_fix "fk_naics"

keep ch_ind ch_ind_new
list if ch_ind == .
drop if ch_ind == .

duplicates drop
recast double ch_ind
replace ch_ind = round(ch_ind, 0.1)
recast double ch_ind_new
replace ch_ind_new = ch_ind if ch_ind_new == .

merge m:1 ch_ind using ${ds_sect}
list if _merge == 1
/* REDACTED 
A few observations are listed based on a ch_ind_new instance deemed to be problematic*/
keep if inlist(_merge, 2, 3)
drop _merge
replace ch_ind_new = ch_ind if ch_ind_new == . // ch_ind_new now has industry code that we want

/* REDACTED 
A miscategorized ch_ind is corrected */

sort ch_ind_new sector
by ch_ind_new sector: gen i_sect = 1 if _n == 1
by ch_ind_new: egen n_sect = total(i_sect)

list ch_ind ch_ind_new sector if n_sect > 1

preserve
/* Sector is updated for some ch_ind instances */

keep ch_ind_new sector
rename ch_ind_new ch_ind
gen ch_ind_rev = 1 // Dummy for new industry codes
save "cw_ind_sect_fk_imp", replace
restore

keep if ch_ind != ch_ind_new
keep ch_ind sector
gen ch_ind_rev = 0 // Dummy for old industry codes that have been discontinued
append using "cw_ind_sect_fk_imp"

duplicates drop
sort ch_ind
isid ch_ind

save "cw_ind_sect_fk_imp", replace
saveold "cw_ind_sect_fk_imp_v12", replace v(12)
export delimited "cw_ind_sect_fk_imp.csv", replace

di "Ended at $S_DATE $S_TIME"
// End of do file
